<?php

namespace app\common\library\supply;

use think\Exception;

/**
 * 存储模块驱动
 */
class Driver
{
    private $config;    // 当前配置
    private $engine;    // 当前供应商引擎类

    protected $error;

    /**
     * 构造方法
     */
    public function __construct($config, $storage = null)
    {
        $this->config = $config;
        // 实例化当前供应商存储引擎
        $this->engine = $this->getEngineClass($storage);
    }


    public function validate(){

    }

    /**
     * 执行直充订单
     */
    public function justRecharge()
    {
        return $this->engine->justRecharge();
    }

    /**
     * 设置请求参数
     */
    public function sendData($param)
    {
        return $this->engine->sendData($param);
    }


    /**
     * 获取错误信息
     */
    public function getError()
    {
        return $this->engine->getError();
    }

    /**
     * 获取当前的存储引擎
     */
    private function getEngineClass($storage = null)
    {
        $engineName = is_null($storage) ? $this->config['default'] : $storage;
        $classSpace = __NAMESPACE__ . '\\engine\\' . ucfirst($engineName);
        if (!class_exists($classSpace)) {
            throw new Exception('未找到供应商引擎类: ' . $engineName);
        }
        return new $classSpace($this->config['engine'][$engineName]);
    }



}
